package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.constant.order.OrderTradeProcessTypeEnum;
import com.ssy.lingxi.order.entity.BaseTradeProcessDO;
import com.ssy.lingxi.order.model.constant.BaseTradeProcessEnum;
import com.ssy.lingxi.order.model.vo.platform.response.PlatformBaseTradeProcessVO;
import com.ssy.lingxi.order.model.vo.process.response.BaseTradeProcessVO;
import com.ssy.lingxi.order.repository.BaseTradeProcessRepository;
import com.ssy.lingxi.order.service.base.IBaseTradeProcessService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础交易流程相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-24
 */
@Service
public class BaseTradeProcessServiceImpl implements IBaseTradeProcessService {
    @Resource
    private BaseTradeProcessRepository baseTradeProcessRepository;

    /**
     * 查询所有交易流程
     *
     * @return 交易流程列表
     */
    @Override
    public List<BaseTradeProcessVO> listProcesses() {
        return baseTradeProcessRepository.findAll(Sort.by("id").ascending()).stream().map(process -> {
            BaseTradeProcessVO processVO = new BaseTradeProcessVO();
            processVO.setBaseProcessid(process.getId());
            processVO.setProcessName(BaseTradeProcessEnum.getNameByCode(process.getCode()));
            processVO.setProcessType(process.getProcessType());
            processVO.setProcessTypeName(OrderTradeProcessTypeEnum.getNameByCode(process.getProcessType()));
            processVO.setDescription(BaseTradeProcessEnum.getRemarkByCode(process.getCode()));
            processVO.setPayTimes(process.getPayTimes());
            return processVO;
        }).collect(Collectors.toList());
    }

    /**
     * 从平台后台查询基础交易流程
     *
     * @return 交易流程列表
     */
    @Override
    public List<PlatformBaseTradeProcessVO> listProcessByPlatform() {
        return baseTradeProcessRepository.findAll(Sort.by("id").ascending()).stream().map(process -> {
            PlatformBaseTradeProcessVO processVO = new PlatformBaseTradeProcessVO();
            processVO.setBaseProcessid(process.getId());
            processVO.setProcessName(BaseTradeProcessEnum.getNameByCode(process.getCode()));
            processVO.setProcessType(process.getProcessType());
            processVO.setProcessTypeName(OrderTradeProcessTypeEnum.getNameByCode(process.getProcessType()));
            processVO.setDescription(BaseTradeProcessEnum.getRemarkByCode(process.getCode()));
            return processVO;
        }).collect(Collectors.toList());
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return 查询结果
     */
    @Override
    public BaseTradeProcessDO findById(Long id) {
        return baseTradeProcessRepository.findById(id).orElse(null);
    }
}
